package dataSystem;

import java.util.List;
import java.util.Arrays;
import java.time.LocalDate;
import salesSystem.model.Order;
import salesSystem.model.OrderItem;

public class DataManagementSystem {
    public static void main(String[] args) {
        System.out.println("=== 数据管理系统启动 ===");
        
        // 1. 初始化数据管理中心
        DataIntegrationCenter dataCenter = new DataIntegrationCenter();
        
        // 2. 模拟销售数据
        OrderItem item1 = new OrderItem("P1001", "笔记本电脑", 1, 5999.00);
        OrderItem item2 = new OrderItem("P1002", "无线鼠标", 2, 99.50);
        List<OrderItem> items = Arrays.asList(item1, item2);
        
        Order order = new Order("CUST001", items, "北京市海淀区中关村大街1号");
        order.setOrderId("ORD20230001");
        order.setOrderDate(LocalDate.now());
        
        // 3. 模拟采购数据
        PurchaseOrder purchase1 = new PurchaseOrder("PO2023001", "SUP001", 
            LocalDate.now().minusDays(5), LocalDate.now(), 
            50, 5500.00, "笔记本电脑", true, false);
        
        PurchaseOrder purchase2 = new PurchaseOrder("PO2023002", "SUP002", 
            LocalDate.now().minusDays(3), LocalDate.now().plusDays(2), 
            100, 85.00, "无线鼠标", true, true);
        
        // 4. 模拟物流数据
        LogisticsInfo logistics1 = new LogisticsInfo("LOG2023001", "PO2023001", 
            "UPS", "123456789", LocalDate.now().minusDays(2), LocalDate.now());
        
        LogisticsInfo logistics2 = new LogisticsInfo("LOG2023002", "ORD20230001", 
            "顺丰", "987654321", LocalDate.now(), LocalDate.now().plusDays(1));
        
        // 5. 导入数据
        dataCenter.importSalesData(Arrays.asList(order));
        dataCenter.importPurchaseData(Arrays.asList(purchase1, purchase2));
        dataCenter.importLogisticsData(Arrays.asList(logistics1, logistics2));
        
        // 6. 生成报表
        ReportGenerator reportGenerator = new ReportGenerator(dataCenter);
        
        System.out.println("\n=== 库存分析报告 ===");
        ReportGenerator.InventoryReport inventoryReport = reportGenerator.generateInventoryReport();
        System.out.printf("库存周转率: %.2f\n", inventoryReport.getTurnoverRate());
        System.out.printf("总销售量: %d\n", inventoryReport.getSalesVolume());
        System.out.printf("平均库存量: %.2f\n", inventoryReport.getAverageInventory());
        
        System.out.println("\n=== 供应商绩效报告 ===");
        ReportGenerator.SupplierReport supplierReport = reportGenerator.generateSupplierReport();
        supplierReport.getSupplierPerformance().forEach((id, perf) -> {
            System.out.printf("供应商 %s - 绩效评分: %.2f (订单数: %d, 准时交付: %d, 质量问题: %d)\n",
                id, perf.getPerformanceScore(), perf.getTotalOrders(),
                perf.getOnTimeDeliveries(), perf.getQualityIssues());
        });
        
        // 7. 数据导出
        try {
            DataExporter.exportToCsv(inventoryReport, "reports/inventory_report.csv");
            DataExporter.exportSupplierReportToCsv(supplierReport, "reports/supplier_performance.csv");
            System.out.println("\n报表已成功导出至reports目录");
        } catch (Exception e) {
            System.err.println("导出报表时出错: " + e.getMessage());
        }
        
        System.out.println("\n=== 数据管理系统运行结束 ===");
    }
}

// 采购订单数据模型
class PurchaseOrder {
    private String orderId;
    private String supplierId;
    private LocalDate orderDate;
    private LocalDate expectedDelivery;
    private int quantity;
    private double unitPrice;
    private String productName;
    private boolean deliveredOnTime;
    private boolean hasQualityIssues;
    
    public PurchaseOrder(String orderId, String supplierId, LocalDate orderDate, 
                        LocalDate expectedDelivery, int quantity, double unitPrice,
                        String productName, boolean deliveredOnTime, boolean hasQualityIssues) {
        this.orderId = orderId;
        this.supplierId = supplierId;
        this.orderDate = orderDate;
        this.expectedDelivery = expectedDelivery;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
        this.productName = productName;
        this.deliveredOnTime = deliveredOnTime;
        this.hasQualityIssues = hasQualityIssues;
    }
    
    // Getter方法
    public String getOrderId() { return orderId; }
    public String getSupplierId() { return supplierId; }
    public LocalDate getOrderDate() { return orderDate; }
    public LocalDate getExpectedDelivery() { return expectedDelivery; }
    public int getQuantity() { return quantity; }
    public double getUnitPrice() { return unitPrice; }
    public String getProductName() { return productName; }
    public boolean isDeliveredOnTime() { return deliveredOnTime; }
    public boolean isHasQualityIssues() { return hasQualityIssues; }
}

// 物流信息数据模型
class LogisticsInfo {
    private String logisticsId;
    private String relatedOrderId;
    private String carrier;
    private String trackingNumber;
    private LocalDate shipmentDate;
    private LocalDate deliveryDate;
    
    public LogisticsInfo(String logisticsId, String relatedOrderId, String carrier,
                       String trackingNumber, LocalDate shipmentDate, LocalDate deliveryDate) {
        this.logisticsId = logisticsId;
        this.relatedOrderId = relatedOrderId;
        this.carrier = carrier;
        this.trackingNumber = trackingNumber;
        this.shipmentDate = shipmentDate;
        this.deliveryDate = deliveryDate;
    }
    
    // Getter方法
    public String getLogisticsId() { return logisticsId; }
    public String getRelatedOrderId() { return relatedOrderId; }
    public String getCarrier() { return carrier; }
    public String getTrackingNumber() { return trackingNumber; }
    public LocalDate getShipmentDate() { return shipmentDate; }
    public LocalDate getDeliveryDate() { return deliveryDate; }
}